package idu0080.veebitoorik.web;

import idu0080.veebitoorik.serviceclient.SOAPClient;
import idu0080.yl6_OpenERPServices_client.Invoice;
import idu0080.yl6_OpenERPServices_client.InvoiceLine;
import idu0080.yl6_OpenERPServices_client.Product;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeliveryServlet
 */
@WebServlet("/delivery")
public class DeliveryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeliveryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String s_invoice_number = null;
	    Invoice selected_invoice = null;

	    SOAPClient soapclient = new SOAPClient();
	    String payment_number = "";
	    Double qty = Double.valueOf(0.0D);
	    
	    if ((request.getParameter("s_invoice_number") != null) && (!request.getParameter("s_invoice_number").equals("")))
	    {
	      s_invoice_number = request.getParameter("s_invoice_number");
	      selected_invoice = soapclient.getInvoiceByNumber(s_invoice_number);

	      InvoiceLine invoice_line = (InvoiceLine) selected_invoice.getListOfInvoiceLines().get(0);
	      Product product = soapclient.getProductById(invoice_line.getProductId());
	      request.setAttribute("selected_invoice", selected_invoice);
	      request.setAttribute("s_invoice_number", s_invoice_number);
	      request.setAttribute("product_name", product.getName());
	    }

	    if ((request.getParameter("qty") != null) && (!request.getParameter("qty").equals("")))
	    {
	      qty = Double.valueOf(Double.parseDouble(request.getParameter("qty")));
	    }

	    if ((request.getParameter("what_to_do").equals("make_delivery")) && (qty.doubleValue() > 0.0D) && (selected_invoice != null))
	    {
	      int delivery_id = soapclient.registerProductDelivery(selected_invoice.getNumber(), qty);
	      if (delivery_id > 0)
	      {
	        request.setAttribute("delivery_id", Integer.toString(delivery_id));
	        selected_invoice = soapclient.getInvoiceByNumber(s_invoice_number);
	        request.setAttribute("selected_invoice", selected_invoice);
	      }
	      else
	      {
	        System.out.println("SOAP_ERROR CODE:" + soapclient.getErrorCode());
	        System.out.println("SOAP_ERROR MESSAGE:" + soapclient.getErrorMessage());
	        request.setAttribute("SOAP_ERROR_CODE", soapclient.getErrorCode());
	        request.setAttribute("SOAP_ERROR_MESSAGE", soapclient.getErrorMessage());
	      }

	    }

	    getServletConfig().getServletContext().getRequestDispatcher("/delivery.jsp").forward(request, response);
	}

}
